package com.ttt.adbgui.gui;

import com.ttt.adbgui.function.*;
import com.ttt.adbgui.jcomp.JFrameComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

/*
 * 首页界面绘制
 * */
@Component
@Slf4j
public class GUIHome {

    @Autowired
    private JFrameComponent jFrameComponent;
    @Autowired
    private TableFill tableFill;
    @Autowired
    private MouseListener listener;


    JFrame jFrame;
    JPanel contentPane;
    JDesktopPane leftDesktopPane;
    JDesktopPane rightDesktopPane;
    // 初始化
    public void init(){
        jFrame.setVisible(true);
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(false);
        String path = GUIHome.class.getClassLoader().getResource("static/R-C.png").getPath();
        if (path.substring(0,5).equals("file:")){
            path = path.substring(6,path.length());
        }
        jFrame.setIconImage(new ImageIcon(path).getImage());
    }
    public void home(){
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setBounds(0, 0, 980, 606);
        // contentPane.setBackground(UIManager.getColor("ComboBox.buttonHighlight"));
        contentPane.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        jFrame.setContentPane(contentPane);
        contentPane.setLayout(null);


        //左边的标题panel
        leftDesktopPane.setBounds(0, 0, 180, 567);
        leftDesktopPane.setBackground(UIManager.getColor("ComboBox.buttonHighlight"));
        contentPane.add(leftDesktopPane);


        // 选择已连接设备
        JLabel jLabelSelect = jFrameComponent.getJLabelSelect();
        jLabelSelect.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/devices32x32.png")));
        jLabelSelect.setHorizontalAlignment(SwingConstants.LEFT);
        jLabelSelect.setBounds(10, 10, 160, 50);
        jLabelSelect.setBackground(UIManager.getColor("ComboBox.buttonHighlight"));
        jLabelSelect.setOpaque(true);
        listener.setSelectDevice(jLabelSelect);
        leftDesktopPane.add(jLabelSelect);


        // 快捷操作
        JLabel devices = jFrameComponent.getJLabelDevices();
        devices.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/shortcut32x32.png")));
        devices.setHorizontalAlignment(SwingConstants.LEFT);
        devices.setBounds(10, 70, 160, 50);
        devices.setBackground(Color.green);
        devices.setOpaque(true);
        // 事件监听
        listener.Shortcut(devices);
        leftDesktopPane.add(devices);


        //文件管理
        JLabel files = jFrameComponent.getJLabelFile();
        files.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/files32x32.png")));
        files.setOpaque(true);
        files.setHorizontalAlignment(SwingConstants.LEFT);
        files.setBounds(10, 130, 160, 50);
        files.setBackground(UIManager.getColor("ComboBox.buttonHighlight"));
        // 事件监听
        listener.fileManager(files);
        leftDesktopPane.add(files);


        // 性能监控
        JLabel jLabelMonitor = jFrameComponent.getJLabelMonitor();
        jLabelMonitor.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/copyfiles32x32.png")));
        jLabelMonitor.setOpaque(true);
        jLabelMonitor.setBounds(10, 190, 160, 50);
        jLabelMonitor.setBackground(UIManager.getColor("ComboBox.buttonHighlight"));
        // 事件监听
        listener.Monitor(jLabelMonitor);
        leftDesktopPane.add(jLabelMonitor);


        // ADB Monkey
        JLabel monkey = jFrameComponent.getJLabelMonkey();
        monkey.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/files32x32.png")));
        monkey.setOpaque(true);
        monkey.setHorizontalAlignment(SwingConstants.LEFT);
        monkey.setBounds(10, 250, 160, 50);
        monkey.setBackground(UIManager.getColor("ComboBox.buttonHighlight"));
        // 事件监听
        listener.monkeyClick(monkey);
        leftDesktopPane.add(monkey);



        // 右边panel
        rightDesktopPane.setBounds(182, 0, 798, 567);
        rightDesktopPane.setBackground(UIManager.getColor("ComboBox.buttonHighlight"));
        contentPane.add(rightDesktopPane);


        // 命令表格
        JTable table = jFrameComponent.getTable();
        setTableBaseAttr(table);
        table.setRowSelectionAllowed(false);
        rightDesktopPane.add(table);
        table.setRowHeight(30);
        table.setBounds(3,40,790,120);
        table.setShowHorizontalLines(false);
        tableFill.tableFillData(table);
        listener.scriptTable(table);


        // 命令行窗口
        JTextPane textPane = jFrameComponent.getTextPane();
        textPane.setBackground(Color.BLACK);
        textPane.setBounds(0,0,777,395);
        textPane.setBorder(null);
        textPane.setForeground(Color.WHITE);
        textPane.setEditable(false);
        textPane.setText(" 示例文本");
        JScrollPane jScrollPane = new JScrollPane(textPane);
        jFrameComponent.setJScrollPane(jScrollPane);
        jScrollPane.setBounds(3,170,777,395);
        jScrollPane.setBorder(null);
        jScrollPane.getVerticalScrollBar().setUI(null);
        rightDesktopPane.add(jScrollPane);


        // 文件预览table
        JTable tableFile = jFrameComponent.getTableFile();
        setTableBaseAttr(tableFile);
        tableFile.setRowHeight(30);
        tableFile.setBounds(0,2,777,563);
        tableFile.setRowSelectionAllowed(false);
        listener.fileTable(tableFile);


        /*
         * 装载table到scroll 然后一起加载到rightPanel中
         *
         * JScrollPane 若没有边框时，会默认给一个边框，所以使用 setBorder(null) 是没有用的
         * 只能使用一个空的边框 BorderFactory.createEmptyBorder();
         * */
        JScrollPane jScrollPaneFile = new JScrollPane(tableFile);
        jFrameComponent.setJScrollPaneFile(jScrollPaneFile);
        jScrollPaneFile.setBounds(3,3,777,562);
        Border emptyBorder = BorderFactory.createEmptyBorder();
        jScrollPaneFile.setBorder(emptyBorder);
        jScrollPaneFile.setVisible(false);
        jScrollPaneFile.getVerticalScrollBar().setUI(null);
        rightDesktopPane.add(jScrollPaneFile);


        /*
        * // 性能监控窗口(试用已过期)
        Engine engine = jFrameComponent.getEngine();
        Browser browser = jFrameComponent.getBrowser();
        // 填充到滚动中
        JScrollPane jScrollPane1 = new JScrollPane(BrowserView.newInstance(browser));
        jFrameComponent.setJScrollEditorPane(jScrollPane1);
        jScrollPane1.setBounds(3,3,777,562);
        jScrollPane1.setBorder(emptyBorder);
        jScrollPane1.setVisible(false);
        jScrollPane1.getVerticalScrollBar().setUI(null);
        rightDesktopPane.add(jScrollPane1);
        * */

    }
    // 填充初始化界面的表格数据
    public void setTableBaseAttr(JTable jTable){
        jTable.getTableHeader().setVisible(false);
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setPreferredSize(new Dimension(0, 0));
        jTable.getTableHeader().setDefaultRenderer(renderer);
        jTable.setBorder(null);
        jTable.setShowVerticalLines(false);
    }
    // 公共组件声明
    public void publicComponent(){
        jFrame = jFrameComponent.getJFrame();
        contentPane = jFrameComponent.getContentPane();
        leftDesktopPane = jFrameComponent.getLeftDesktopPane();
        rightDesktopPane = jFrameComponent.getRightDesktopPane();
        home();
        jFrame.dispose();
        init();
    }
}
